package com.wxy.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.common.core.domain.AjaxResult;
import com.wxy.common.utils.DozerUtil;
import com.wxy.common.utils.ServletUtils;
import com.wxy.web.dto.UserlogDto;
import com.wxy.web.model.Module;
import com.wxy.web.model.User;
import com.wxy.web.model.Userlog;
import com.wxy.web.service.IModuleService;
import com.wxy.web.service.IUserService;
import com.wxy.web.service.IUserlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户日志 前端控制器
 * </p>
 *
 * @author wxy
 * @since 2021-06-21
 */
@RestController
@RequestMapping("/admin/userlog")
public class UserlogController {
   @Autowired
   public DozerUtil dozerUtil;
   @Autowired
   private IUserlogService userlogService;
   @Autowired
   private IUserService userService;
   @Autowired
   private IModuleService moduleService;
   @GetMapping("list")
   public AjaxResult list(Userlog model)
   {
      Integer pageNum = ServletUtils.getParameterToInt("page",1);
      Integer pageSize = ServletUtils.getParameterToInt("limit",10);

      LambdaQueryWrapper<Userlog> lambdaQueryWrapper = Wrappers.lambdaQuery();
      //if(StringUtils.isNotEmpty(model.getName()))
      //{
      //   lambdaQueryWrapper.like(Userlog::getName , model.getName());
      //}

      Page<Userlog> page = new Page<>(pageNum , pageSize);
      page.addOrder(OrderItem.desc("id"));
      IPage<Userlog> iPage = this.userlogService.page(page,lambdaQueryWrapper);

      HashMap<String,Object> map = new HashMap<>();

      List<UserlogDto> list = new ArrayList<>();
      for (Userlog record : iPage.getRecords()) {
         UserlogDto userlogDto = dozerUtil.map(record, UserlogDto.class);

         User user = userService.getById(userlogDto.getUserId());
         if(user!=null){
            userlogDto.setUsername(user.getUsername());
            userlogDto.setFullname(user.getFullname());
         }

         Module module = moduleService.getById(userlogDto.getModuleId());
         if(module != null){
            userlogDto.setModuleUrl(module.getUrl());
            userlogDto.setModuleName(module.getName());
         }

         list.add(userlogDto);
      }

      map.put("list",list);
      map.put("count",iPage.getTotal());

      AjaxResult result = AjaxResult.success();
      result.put("data",map);
      return result;
   }


   @GetMapping("info")
   public AjaxResult info(Integer id)
   {
      Userlog model = userlogService.getById(id);

      UserlogDto userlogDto = dozerUtil.map(model,UserlogDto.class);

      User user = userService.getById(userlogDto.getUserId());
      if(user!=null){
         userlogDto.setUsername(user.getUsername());
         userlogDto.setFullname(user.getFullname());
      }

      Module module = moduleService.getById(userlogDto.getModuleId());
      if(module != null){
         userlogDto.setModuleUrl(module.getUrl());
         userlogDto.setModuleName(module.getName());
      }

      AjaxResult result = AjaxResult.success();
      result.put("data",userlogDto);
      return result;
   }

   @PostMapping("add")
   public AjaxResult add(@Validated @RequestBody Userlog model)
   {
      Boolean success = userlogService.save(model);
      AjaxResult ajaxResult = null;
      if(success)
      ajaxResult = AjaxResult.success();
      else
      ajaxResult = AjaxResult.error();

      return ajaxResult;
   }

   @PostMapping("edit")
   public AjaxResult edit(@Validated @RequestBody Userlog model)
   {
      Boolean success = userlogService.updateById(model);
      AjaxResult ajaxResult = null;
      if(success)
         ajaxResult = AjaxResult.success();
      else
         ajaxResult = AjaxResult.error();
      return ajaxResult;
   }

   @PostMapping("del/{id}")
   public AjaxResult del(@PathVariable Integer id)
   {
      Boolean success = userlogService.removeById(id);
      AjaxResult ajaxResult = null;
      if(success)
         ajaxResult = AjaxResult.success();
      else
         ajaxResult = AjaxResult.error();
      return ajaxResult;
   }

}
